//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "FdmBlackScholesMesher.h"
using namespace Cephei::QL::Experimental::Finitedifferences;
#include <gen/QL/Processes/GeneralizedBlackScholesProcess.h>
#include <gen/QL/Quote.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Experimental/Finitedifferences/Fdm1dMesher.h>
using namespace Cephei::QL::Processes;
using namespace Cephei::QL;
using namespace Cephei::QL::Termstructures;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Finitedifferences::CFdmBlackScholesMesher::CFdmBlackScholesMesher (boost::shared_ptr<QuantLib::FdmBlackScholesMesher>& childNative, Object^ owner) : CFdm1dMesher(CFdmBlackScholesMesher::typeid)
{
#ifdef HANDLE
	_phFdmBlackScholesMesher = NULL;
#endif
	_ppFdmBlackScholesMesher = &childNative;
    _ppFdm1dMesher = new boost::shared_ptr<QuantLib::Fdm1dMesher> (boost::dynamic_pointer_cast<QuantLib::Fdm1dMesher> (*_ppFdmBlackScholesMesher));
}
Cephei::QL::Experimental::Finitedifferences::CFdmBlackScholesMesher::CFdmBlackScholesMesher (QuantLib::FdmBlackScholesMesher& childNative, Object^ owner) : CFdm1dMesher(CFdmBlackScholesMesher::typeid)
{
#ifdef HANDLE
	_phFdmBlackScholesMesher = NULL;
#endif
	_ppFdmBlackScholesMesher = new boost::shared_ptr<QuantLib::FdmBlackScholesMesher> (&childNative);
    _ppFdm1dMesher = new boost::shared_ptr<QuantLib::Fdm1dMesher> (boost::dynamic_pointer_cast<QuantLib::Fdm1dMesher> (*_ppFdmBlackScholesMesher));
    _FdmBlackScholesMesherOwner = owner;
    _Fdm1dMesherOwner = owner;
}

Cephei::QL::Experimental::Finitedifferences::CFdmBlackScholesMesher::CFdmBlackScholesMesher (CFdmBlackScholesMesher^ copy) : CFdm1dMesher(CFdmBlackScholesMesher::typeid)
{
#ifdef HANDLE
	_phFdmBlackScholesMesher = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFdmBlackScholesMesher = new boost::shared_ptr<QuantLib::FdmBlackScholesMesher> (copy->GetShared());
        _ppFdm1dMesher = new boost::shared_ptr<QuantLib::Fdm1dMesher> (boost::dynamic_pointer_cast<QuantLib::Fdm1dMesher> (*_ppFdmBlackScholesMesher));
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdmBlackScholesMesher::CFdmBlackScholesMesher (System::Type^ t) : CFdm1dMesher(CFdmBlackScholesMesher::typeid)
{
#ifdef HANDLE
	_phFdmBlackScholesMesher = NULL;
#endif
	if (!t->IsSubclassOf(CFdmBlackScholesMesher::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Finitedifferences::CFdmBlackScholesMesher::CFdmBlackScholesMesher (QuantLib::Handle<QuantLib::FdmBlackScholesMesher>& childNative, Object^ owner)  : CFdm1dMesher(CFdmBlackScholesMesher::typeid)
{
	_phFdmBlackScholesMesher = &childNative;
	_ppFdmBlackScholesMesher = &static_cast<boost::shared_ptr<QuantLib::FdmBlackScholesMesher>>(childNative.currentLink());
    _ppFdm1dMesher = new boost::shared_ptr<QuantLib::Fdm1dMesher> (boost::dynamic_pointer_cast<QuantLib::Fdm1dMesher> (*_ppFdmBlackScholesMesher));
    _FdmBlackScholesMesherOwner = owner;
}
Cephei::QL::Experimental::Finitedifferences::CFdmBlackScholesMesher::CFdmBlackScholesMesher (QuantLib::Handle<QuantLib::FdmBlackScholesMesher> childNative)  : CFdm1dMesher(CFdmBlackScholesMesher::typeid)
{
	_phFdmBlackScholesMesher = &childNative;
	_ppFdmBlackScholesMesher = &static_cast<boost::shared_ptr<QuantLib::FdmBlackScholesMesher>>(childNative.currentLink());
    _ppFdm1dMesher = new boost::shared_ptr<QuantLib::Fdm1dMesher> (boost::dynamic_pointer_cast<QuantLib::Fdm1dMesher> (*_ppFdmBlackScholesMesher));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Finitedifferences::CFdmBlackScholesMesher::CFdmBlackScholesMesher (QuantLib::FdmBlackScholesMesher childNative)  : CFdm1dMesher(CFdmBlackScholesMesher::typeid)
{
#ifdef HANDLE
	_phFdmBlackScholesMesher = NULL;
#endif
	_ppFdmBlackScholesMesher = new boost::shared_ptr<QuantLib::FdmBlackScholesMesher> (new QuantLib::FdmBlackScholesMesher (childNative));
    _ppFdm1dMesher = new boost::shared_ptr<QuantLib::Fdm1dMesher> (boost::dynamic_pointer_cast<QuantLib::Fdm1dMesher> (*_ppFdmBlackScholesMesher));
}
#endif

Cephei::QL::Experimental::Finitedifferences::CFdmBlackScholesMesher::~CFdmBlackScholesMesher ()
{
    if (_ppFdmBlackScholesMesher != NULL)
    {
	    delete _ppFdmBlackScholesMesher;
        _ppFdmBlackScholesMesher = NULL;
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdmBlackScholesMesher::!CFdmBlackScholesMesher ()
{
    if (_ppFdmBlackScholesMesher != NULL)
    {
	    delete _ppFdmBlackScholesMesher;
    }
}
QuantLib::FdmBlackScholesMesher& Cephei::QL::Experimental::Finitedifferences::CFdmBlackScholesMesher::GetReference ()
{
    if (_ppFdmBlackScholesMesher == NULL) throw gcnew NativeNullException ();
	return **_ppFdmBlackScholesMesher;
}
boost::shared_ptr<QuantLib::FdmBlackScholesMesher>& Cephei::QL::Experimental::Finitedifferences::CFdmBlackScholesMesher::GetShared ()
{
    if (_ppFdmBlackScholesMesher == NULL) throw gcnew NativeNullException ();
	return *_ppFdmBlackScholesMesher;
}
QuantLib::FdmBlackScholesMesher* Cephei::QL::Experimental::Finitedifferences::CFdmBlackScholesMesher::GetPointer ()
{
    if (_ppFdmBlackScholesMesher == NULL) throw gcnew NativeNullException ();
	return &**_ppFdmBlackScholesMesher;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FdmBlackScholesMesher>& Cephei::QL::Experimental::Finitedifferences::CFdmBlackScholesMesher::GetHandle ()
{
	if (_phFdmBlackScholesMesher == NULL)
	{
		_phFdmBlackScholesMesher = new Handle<QuantLib::FdmBlackScholesMesher> (*_ppFdmBlackScholesMesher);
	}
	return *_phFdmBlackScholesMesher;
}
#endif
bool Cephei::QL::Experimental::Finitedifferences::CFdmBlackScholesMesher::HasNative () 
{
	return (_ppFdmBlackScholesMesher != NULL);
}

Cephei::QL::Processes::IGeneralizedBlackScholesProcess^ Cephei::QL::Experimental::Finitedifferences::CFdmBlackScholesMesher::ProcessHelper (Cephei::QL::IQuote^ s0, Cephei::QL::Termstructures::IYieldTermStructure^ rTS, Cephei::QL::Termstructures::IYieldTermStructure^ qTS, Double vol)
{
    CQuote^ _Cs0;
    CYieldTermStructure^ _CrTS;
    CYieldTermStructure^ _CqTS;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cs0 = safe_cast<CQuote^> (s0);
        _Cs0->Lock();
        Handle<QuantLib::Quote>& _s0 = static_cast<Handle<QuantLib::Quote>&> (_Cs0->GetHandle ()); 
        _CrTS = safe_cast<CYieldTermStructure^> (rTS);
        _CrTS->Lock();
        Handle<QuantLib::YieldTermStructure>& _rTS = static_cast<Handle<QuantLib::YieldTermStructure>&> (_CrTS->GetHandle ()); 
        _CqTS = safe_cast<CYieldTermStructure^> (qTS);
        _CqTS->Lock();
        Handle<QuantLib::YieldTermStructure>& _qTS = static_cast<Handle<QuantLib::YieldTermStructure>&> (_CqTS->GetHandle ()); 
        QuantLib::Volatility _vol = (QuantLib::Volatility)ValueHelper::Convert (vol);
    	boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess> _rv = (boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess>)(*_ppFdmBlackScholesMesher)->processHelper ( _s0,  _rTS,  _qTS,  _vol );   
        Cephei::QL::Processes::CGeneralizedBlackScholesProcess^ _nrv = gcnew Cephei::QL::Processes::CGeneralizedBlackScholesProcess (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cs0 != nullptr) _Cs0->Unlock();
        if (_CrTS != nullptr) _CrTS->Unlock();
        if (_CqTS != nullptr) _CqTS->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

